@page "/publish-announcement"
@using System.Text.Json
@using Blazor.Model
@inject IStringLocalizer<PublishAnnouncementLocalization> Loc
@inject IStringLocalizer<CommonLocalization> CommonLoc
@inject INotificationService Notice

<Title Level="1">@Loc["title"]</Title>

<Paragraph>@Loc["description"]</Paragraph>

<Form Model="@_model" OnFinish="OnFinish" LabelColSpan="4" WrapperColSpan="20" Style="width: 1000px" LabelAlign="AntLabelAlignType.Left">
    <FormItem Label="@Loc["channel"]">
        <RadioGroup @bind-Value="@context.Channel" ButtonStyle="RadioButtonStyle.Solid">
            @foreach (var c in Enum.GetValues<Channel>())
            {
                <Radio RadioButton Value="@c">@c.ToString()</Radio>
            }
        </RadioGroup>
    </FormItem>
    <FormItem Label="@Loc["content"]">
        <TextArea @bind-Value="@context.Content" AutoSize MinRows="4"></TextArea>
    </FormItem>
    <FormItem WrapperColOffset="4">
        <Button HtmlType="submit" Type="@ButtonType.Primary">@CommonLoc["confirm"]</Button>
    </FormItem>
</Form>

@code {
    public class Model
    {
        public string Content { get; set; } = string.Empty;
        public Channel Channel { get; set; }
    }

    private class Announcement
    {
        public string Content { get; set; }
    }

    private readonly Model _model = new();

    private async Task OnFinish(EditContext context)
    {
        var announcement = new Announcement()
        {
            Content = _model.Content,
        };
        var s = JsonSerializer.Serialize(announcement);
        Console.WriteLine(s);
        using var memoryStream = new MemoryStream();
        await JsonSerializer.SerializeAsync(memoryStream, announcement, new JsonSerializerOptions()
        {
            WriteIndented = true,
        });
        
        {
            memoryStream.Seek(0, SeekOrigin.Begin);
            var result = true;

            if (result)
            {
                await Notice.Open(new NotificationConfig()
                {
                    Message = "Success",
                    NotificationType = NotificationType.Success,
                });
            }
            else
            {
                await Notice.Open(new NotificationConfig()
                {
                    Message = "Failed",
                    NotificationType = NotificationType.Error,
                });
            }
        }
    }
}